import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: 0,
        userName: "",
        show: false
    },
    async onShow() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("sign-in")
            .where({
                alliance: app.data.alliance,
                [app.data.act]: '待定'
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
            });
        await db.collection("controlSign-in")
            .where({
                alliance: app.data.alliance,
                [app.data.act]: '待定'
            })
            .get()
            .then(res => {
                let member = This.data.member;
                res.data.map((value) => {
                    value.control = true;
                    member.push(value);
                })
                This.setData({ member: member });
            });
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    async recover(e) {
        let This = this;
        await Dialog.confirm({
                title: '提示',
                message: '请确认该成员是否参加战役',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false;
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId),
                    // alliance: app.data.alliance
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    if (e.currentTarget.dataset.control) {
                        db.collection("controlSign-in")
                            .where({
                                userName: e.target.dataset.username,
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [app.data.act]: _.set("√")
                                }
                            })
                            .then(async(res) => {
                                Toast('补录成功');
                                await db.collection("sign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                await db.collection("controlSign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            }).catch(err => {
                                Toast('补录失败请重新补录');
                            })
                    } else {
                        db.collection("sign-in")
                            .where({
                                userName: e.target.dataset.username,
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [app.data.act]: _.set("√")
                                }
                            })
                            .then(async(res) => {
                                Toast('补录成功');
                                await db.collection("sign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                await db.collection("controlSign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            }).catch(err => {
                                Toast('补录失败请重新补录');
                            })
                    }
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },
    async refuse(e) {
        let This = this;
        await Dialog.confirm({
                title: '提示',
                message: '请确认是否拒绝该成员打卡申请',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false;
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId),
                    // alliance: app.data.alliance
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                }).catch((err) => {
                    console.log(err);
                })
                if (admin) {
                    if (e.currentTarget.dataset.control) {
                        db.collection("controlSign-in")
                            .where({
                                userName: e.target.dataset.username,
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [app.data.act]: _.set("×")
                                }
                            })
                            .then(async(res) => {
                                Toast('补录成功');
                                await db.collection("sign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                await db.collection("controlSign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            }).catch(err => {
                                Toast('补录失败请重新补录');
                            })
                    } else {
                        db.collection("sign-in")
                            .where({
                                userName: e.target.dataset.username,
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [app.data.act]: _.set("×")
                                }
                            })
                            .then(async(res) => {
                                Toast('补录成功');
                                console.log(2, res);
                                await db.collection("sign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                await db.collection("controlSign-in").where({
                                        [app.data.act]: '待定',
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            }).catch(err => {
                                Toast('补录失败请重新补录');
                            })
                    }

                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },
    async showSrc(event) {
        this.setData({ show: true });
        this.setData({ userName: event.currentTarget.dataset.username });
        this.setData({ iconSrc: "" });
        await qq.cloud.downloadFile({
            fileID: `cloud://ww-8g97rzjc90e04d38.7777-ww-8g97rzjc90e04d38-1304817035/${event.currentTarget.dataset.username}/${app.data.act}.jpg`,
            success: res => {
                this.setData({ iconSrc: res.tempFilePath });
            },
            fail: err => {
                console.log(err);
            }
        });
    },
    onClickHide() {
        this.setData({ show: false });
    },
    async oneKey() {
        const db = qq.cloud.database();
        const _ = db.command;
        const This = this;
        await Dialog.confirm({
            title: '提示',
            message: '请确认是否一键补录',
        }).then(() => {
            let list = [];
            this.data.member.map((value, index) => {
                db.collection("sign-in")
                    .where({
                        userName: value.username,
                        unionId: value.unionId,
                        alliance: app.data.alliance
                    })
                    .update({
                        data: {
                            [app.data.act]: _.set("√")
                        }
                    })
                db.collection("controlSign-in")
                    .where({
                        userName: value.username,
                        unionId: value.unionId,
                        alliance: app.data.alliance
                    })
                    .update({
                        data: {
                            [app.data.act]: _.set("√")
                        }
                    })
            });
            Promise.all(list)
                .then(async(res) => {
                    Toast('补录成功');
                    await db.collection("sign-in").where({
                            [app.data.act]: '待定',
                            alliance: app.data.alliance
                        })
                        .get()
                        .then(res => {
                            This.setData({ member: res.data });
                        });
                    await db.collection("controlSign-in").where({
                            [app.data.act]: '待定',
                            alliance: app.data.alliance
                        })
                        .get()
                        .then(res => {
                            let member = This.data.member;
                            res.data.map((value) => {
                                value.control = true;
                                member.push(value);
                            })
                            This.setData({ member: member });
                        });
                })
                .catch((err) => { console.log(err); });
        }).catch(() => {

        })
    }

})